﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace Systems
{
    public class ObjectManager
    {
        List<Objects.BaseObject> GameObjects;
        public ObjectManager()
        {
            GameObjects = new List<Objects.BaseObject>();
        }
        /// <summary>
        /// Returns the number of objects in the object manager;
        /// </summary>
        /// <returns></returns>
        public int NumberOfObjects()
        {
            return GameObjects.Count;
        }

        /// <summary>
        /// Returns all the objects in the object manager;
        /// </summary>
        /// <returns></returns>
        public List<Objects.BaseObject> GetObjects()
        {
            return GameObjects;
        }
        /// <summary>
        /// Adds an object to the object manager
        /// </summary>
        /// <param name="theObject">The object to add</param>
        public void AddObject(Objects.BaseObject theObject)
        {
            GameObjects.Add(theObject);
        }

        /// <summary>
        /// Removes an object from the object manager
        /// </summary>
        /// <param name="theObject">The object to remove</param>
        public void RemoveObject(Objects.BaseObject theObject)
        {
            GameObjects.Remove(theObject);
        }

        /// <summary>
        /// Disposes of all the objects in the object manager and shuts
        /// down the object manager
        /// </summary>
        public void Shutdown()
        {
            GameObjects.Clear();
        }


        /// <summary>
        /// Runs the check collisions function for objects
        /// </summary>
        public void CheckCollisions()
        {
            for (int currentObject = 0; currentObject < GameObjects.Count; currentObject++)
            {
                for (int collidingObject = 0; collidingObject < GameObjects.Count; collidingObject++)
                {
                    GameObjects[currentObject].CheckCollision(GameObjects[collidingObject]);
                }
            }
        }

        /// <summary>
        /// Updates all the objects in the object manager.
        /// </summary>
        /// <param name="ElapsedTime">The elapsed time</param>
        public void Update(float ElapsedTime)
        {
            CheckCollisions();

            for (int currentObject = 0; currentObject < GameObjects.Count; currentObject++)
            {
                if (GameObjects[currentObject].IsActive == true)
                {

                    GameObjects[currentObject].Update(ElapsedTime);

                }
                else
                {
                    GameObjects.RemoveAt(currentObject);
                }
            }
        }

        /// <summary>
        /// Renders all the objects in the object manager
        /// </summary>
        public void Render(float ElapsedTime)
        {
            for (int i = 0; i < GameObjects.Count; i++)
            {
                GameObjects[i].Render(ElapsedTime);
            }
        }
    }
}